<template>
  <div>
    <!-- 添加用户对话框 -->
    <el-dialog
      :title="(current?.username || '') + '的信息'"
      :visible.sync="open"
      width="50%"
      :before-close="handleClose"
    >
      <!-- 内容主体区 -->
      <el-form
        :model="current"
        :rules="Rules"
        ref="currentRef"
        label-width="100px"
      >
        <el-form-item label="用户名称" prop="username">
          <el-input
            v-model="current.username"
            :disabled="isDisabled || this.type === 2"
          ></el-input>
        </el-form-item>
        <el-form-item label="密码" v-if="type === 3" prop="password">
          <el-input v-model="current.password"></el-input>
        </el-form-item>
        <el-form-item label="邮箱" prop="email">
          <el-input v-model="current.email" :disabled="isDisabled"></el-input>
        </el-form-item>
        <el-form-item label="手机" prop="mobile">
          <el-input v-model="current.mobile" :disabled="isDisabled"></el-input>
        </el-form-item>

        <el-form-item prop="sex" label="性别">
          <el-radio-group v-model="current.sex">
            <el-radio
              :disabled="isDisabled"
              v-for="item in __SEX_OPTIONS__"
              :key="item.value"
              :label="item.value"
              >{{ item.label }}</el-radio
            >
          </el-radio-group>
        </el-form-item>
      </el-form>
      <!-- 底部 -->
      <span slot="footer" class="dialog-footer">
        <el-button @click="handleClose">取 消</el-button>
        <el-button type="primary" @click="handleOk">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: 'userDialogs',
  props: {
    // 当前编辑对象
    current: {},
    // 1 查看 2 编辑 3代表新增
    type: Number,
    // 弹窗是否打开
    open: Boolean,
  },
  data() {
    // 验证邮箱的规则
    var checkEmail = (rule, value, callback) => {
      const regEmail = /^\w+@\w+(\.\w+)+$/
      if (!regEmail.test(value)) {
        callback(new Error('请输入合法的邮箱'))
      }
      callback()
    }
    // 验证手机号的规则
    var checkMobile = (rule, value, callback) => {
      const regMobile = /^1[34578]\d{9}$/
      if (!regMobile.test(value)) {
        callback(new Error('请输入合法的电话'))
      }
      callback()
    }
    return {
      addDialogVisible: false,
      currentForm: {
        username: '',
        password: '',
        email: '',
        mobile: '',
        sex: null,
      },
      // 添加用户的表单规则对象
      Rules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          {
            min: 3,
            max: 10,
            message: '用户名长度在3~10个字符',
            trigger: 'blur',
          },
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          {
            min: 6,
            max: 15,
            message: '密码长度在6~15个字符',
            trigger: 'blur',
          },
        ],
        sex: [{ required: true, message: '请选择一个性别', trigger: 'blur' }],
        email: [
          { required: true, message: '请输入用邮箱', trigger: 'blur' },
          { validator: checkEmail, trigger: 'blur' },
        ],
        mobile: [
          { required: true, message: '请输入用手机', trigger: 'blur' },
          { validator: checkMobile, trigger: 'blur' },
        ],
      },
    }
  },

  computed: {
    isDisabled() {
      return this.type === 1
    },
  },
  watch: {
    current(newValue) {
      this.current = newValue
    },
  },
  methods: {
    handleClose() {
      this.$refs.currentRef.resetFields()
      this.$emit('handleClose')
    },
    handleOk() {
      this.$refs.currentRef.validate((valid) => {
        if (valid) {
          this.$emit('handleOk', this.current)
          this.$refs.currentRef.resetFields()
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
  },
}
</script>
